;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; package: cl-soap -*-
;;;;
;;;; $Id: $
;;;;
;;;; The environment of defined xsd types, elements and namespaces/namespace maps
;;;; in the CL-SOAP package
;;;;
;;;; Copyright (C) 2007 Utz-Uwe Haus. All Rights Reserved.
;;;;
;;;; You are granted the rights to distribute and use this software
;;;; as governed by the terms of the Lisp Lesser GNU Public License
;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
;;;;

(in-package :cl-soap)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (initialize-soap-environment))


;;; Global state for the xsd engine: declared types and elements
(defstruct xsd-environment
  (type-for-symbol (make-hash-table :test #'eq) :type hash-table)
  (element-for-symbol (make-hash-table :test #'eq) :type hash-table)
  (namespaces (make-hash-table :test #'equalp) :type hash-table))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (setf (documentation 'xsd-environment 'type)
	"Datastructure for saving symbol->schema-element and symbol->schema-type as well as namespace maps."))

(defvar *defined-xsd-elements* 
  (make-xsd-environment)
  "Global state of the CL-SOAP package")

(defun new-soap-environment ()
  "Clean up CL-SOAP state."
  ;;; clean things:
  ;; follow namespace maps and delete packages that appear
  (loop :for uri :being :each :hash-key :in (xsd-environment-namespaces *defined-xsd-elements*)
	:do (s-xml:unregister-namespace uri :delete-package T :unknown-ok T :verbose T)
	:finally (clrhash (xsd-environment-namespaces *defined-xsd-elements*)))
  ;; kill elements and types
  (clrhash (xsd-environment-type-for-symbol *defined-xsd-elements*))
  (clrhash (xsd-environment-element-for-symbol *defined-xsd-elements*))
  
  ;; re-initialize
  (initialize-soap-environment)
  ;; return nothing
  (values))
